package com.pr.makfilm.service.impl;

import com.pr.makfilm.dao.IAbstractDao;
import com.pr.makfilm.dao.IRoomDao;
import com.pr.makfilm.domain.*;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.service.util.SidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 17.09.13
 * Time: 23:01
 * To change this template use File | Settings | File Templates.
 */
@Service(RoomServiceImpl.BEAN_NAME)
public class RoomServiceImpl extends AbstractServiceImpl<Room> implements IRoomService
{
   public static final String BEAN_NAME = "roomService";

   @Resource
   private IRoomDao roomDao;

   @Resource
   private ISecurityService securityService;

   @Override
   protected IAbstractDao<Room> getDao()
   {
      return roomDao;
   }

   @Override
   @Transactional
   public Room save(Room room)
   {
      if (StringUtils.isEmpty(room.getSid()))
      {
         room.setSid(SidUtils.generate());
      }
      room.setUser(securityService.getLoggedInUser());
      return super.save(room);
   }

   @Override
   @Transactional
   public List<String> findRoomCities()
   {
      User user = securityService.getLoggedInUser();
      return roomDao.findRoomCities(user);
   }

   @Override
   @Transactional(readOnly = false)
   public Room create(Room room)
   {
      room.setObjectState(ObjectStateEnum.INACTIVE);
      room.setyOrder(NumberOrderEnum.BEGINNING);
      room.setyStyle(NumberStyleEnum.ALPHABETIC);
      room.setxOrder(NumberOrderEnum.BEGINNING);
      room.setxStyle(NumberStyleEnum.ARABIC_NUMERIC);
      return save(room);
   }
}
